use rand::Rng;

pub fn generate() -> String {
    let mut rng = rand::thread_rng();

    // Generate first 14 digits randomly
    let mut imei = [0u8; 15];
    for i in 0..14 {
        imei[i] = rng.gen_range(0..10);
    }

    // Calculate the Luhn checksum digit
    let mut sum = 0;
    for (i, &digit) in imei.iter().enumerate().take(14) {
        let mut digit = digit;
        if i % 2 == 1 {
            digit *= 2;
            if digit > 9 {
                digit -= 9;
            }
        }
        sum += digit;
    }
    imei[14] = (10 - (sum % 10)) % 10;

    imei.iter().map(|&digit| digit.to_string()).collect()
}